home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / f2c / may_5_92.lha / f2c.VMay_5_1992 / src / niceprintf.c < prev    next >
C/C++ Source or Header  |  1992-05-07  |  9KB  |  372 lines

  1. /****************************************************************
  2. Copyright 1990, 1991 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25. #include "names.h"
  26. #include "output.h"
  27.  
  28. #define TOO_LONG_INDENT (2 * tab_size)
  29. #define MAX_INDENT 44
  30. #define MIN_INDENT 22
  31. static int last_was_newline = 0;
  32. int indent = 0;
  33. int in_comment = 0;
  34.  extern int gflag1;
  35.  extern char *file_name;
  36.  
  37.  static int
  38. write_indent(fp, use_indent, extra_indent, start, end)
  39.  FILE *fp;
  40.  int use_indent, extra_indent;
  41.  char *start, *end;
  42. {
  43.     int ind, tab;
  44.  
  45.     if (gflag1 && last_was_newline)
  46.     fprintf(fp, "#line %ld \"%s\"\n", lineno, file_name);
  47.     if (last_was_newline && use_indent) {
  48.     if (*start == '\n') do {
  49.         putc('\n', fp);
  50.         if (++start > end)
  51.             return;
  52.         }
  53.         while(*start == '\n');
  54.  
  55.     ind = indent <= MAX_INDENT
  56.         ? indent
  57.         : MIN_INDENT + indent % (MAX_INDENT - MIN_INDENT);
  58.  
  59.     tab = ind + extra_indent;
  60.  
  61.     while (tab > 7) {
  62.         putc ('\t', fp);
  63.         tab -= 8;
  64.     } /* while */
  65.  
  66.     while (tab-- > 0)
  67.         putc (' ', fp);
  68.     } /* if last_was_newline */
  69.  
  70.     while (start <= end)
  71.     putc (*start++, fp);
  72. } /* write_indent */
  73.  
  74.  
  75. /*VARARGS2*/
  76. int margin_printf (fp, a, b, c, d, e, f, g)
  77. FILE *fp;
  78. char *a;
  79. long b, c, d, e, f, g;
  80. {
  81.     ind_printf (0, fp, a, b, c, d, e, f, g);
  82. } /* margin_printf */
  83.  
  84. /*VARARGS2*/
  85. int nice_printf (fp, a, b, c, d, e, f, g)
  86. FILE *fp;
  87. char *a;
  88. long b, c, d, e, f, g;
  89. {
  90.     ind_printf (1, fp, a, b, c, d, e, f, g);
  91. } /* nice_printf */
  92.  
  93.  
  94. #define  max_line_len c_output_line_length
  95.          /* 74Number of characters allowed on an output
  96.                        line.  This assumes newlines are handled
  97.                        nicely, i.e. a newline after a full text
  98.                        line on a terminal is ignored */
  99.  
  100. /* output_buf   holds the text of the next line to be printed.  It gets
  101.    flushed when a newline is printed.   next_slot   points to the next
  102.    available location in the output buffer, i.e. where the next call to
  103.    nice_printf will have its output stored */
  104.  
  105. static char *output_buf;
  106. static char *next_slot;
  107. static char *string_start;
  108.  
  109. static char *word_start = NULL;
  110. static int cursor_pos = 0;
  111. static int In_string = 0;
  112.  
  113.  void
  114. np_init()
  115. {
  116.     next_slot = output_buf = Alloc(MAX_OUTPUT_SIZE);
  117.     memset(output_buf, 0, MAX_OUTPUT_SIZE);
  118.     }
  119.  
  120.  static char *
  121. adjust_pointer_in_string(pointer)
  122.  register char *pointer;
  123. {
  124.     register char *s, *s1, *se, *s0;
  125.  
  126.     /* arrange not to break \002 */
  127.     s1 = string_start ? string_start : output_buf;
  128.     for(s = s1; s < pointer; s++) {
  129.         s0 = s1;
  130.         s1 = s;
  131.         if (*s == '\\') {
  132.             se = s++ + 4;
  133.             if (se > pointer)
  134.                 break;
  135.             if (*s < '0' || *s > '7')
  136.                 continue;
  137.             while(++s < se)
  138.                 if (*s < '0' || *s > '7')
  139.                     break;
  140.             --s;
  141.             }
  142.         }
  143.     return s0 - 1;
  144.     }
  145.  
  146. /* ANSI says strcpy's behavior is undefined for overlapping args,
  147.  * so we roll our own fwd_strcpy: */
  148.  
  149.  static void
  150. fwd_strcpy(t, s)
  151.  register char *t, *s;
  152. { while(*t++ = *s++); }
  153.  
  154. /* isident -- true iff character could belong to a unit.  C allows
  155.    letters, numbers and underscores in identifiers.  This also doubles as
  156.    a check for numeric constants, since we include the decimal point and
  157.    minus sign.  The minus has to be here, since the constant "10e-2"
  158.    cannot be broken up.  The '.' also prevents structure references from
  159.    being broken, which is a quite acceptable side effect */
  160.  
  161. #define isident(x) (Tr[x] & 1)
  162. #define isntident(x) (!Tr[x])
  163.  
  164. int ind_printf (use_indent, fp, a, b, c, d, e, f, g)
  165. int use_indent;
  166. FILE *fp;
  167. char *a;
  168. long b, c, d, e, f, g;
  169. {
  170.     extern int max_line_len;
  171.     extern FILEP c_file;
  172.     extern char tr_tab[];    /* in output.c */
  173.     register char *Tr = tr_tab;
  174.     int ch, inc, ind;
  175.     static int extra_indent, last_indent, set_cursor = 1;
  176.  
  177.     cursor_pos += indent - last_indent;
  178.     last_indent = indent;
  179.     sprintf (next_slot, a, b, c, d, e, f, g);
  180.  
  181.     if (fp != c_file) {
  182.     fprintf (fp,"%s", next_slot);
  183.     return 1;
  184.     } /* if fp != c_file */
  185.  
  186.     do {
  187.     char *pointer;
  188.  
  189. /* The   for   loop will parse one output line */
  190.  
  191.     if (set_cursor) {
  192.         ind = indent <= MAX_INDENT
  193.             ? indent
  194.             : MIN_INDENT + indent % (MAX_INDENT - MIN_INDENT);
  195.         cursor_pos = ind + extra_indent;
  196.         set_cursor = 0;
  197.         }
  198.     if (in_comment)
  199.             for (pointer = next_slot; *pointer && *pointer != '\n' &&
  200.                 cursor_pos <= max_line_len; pointer++)
  201.             cursor_pos++;
  202.     else
  203.           for (pointer = next_slot; *pointer && *pointer != '\n' &&
  204.         cursor_pos <= max_line_len; pointer++) {
  205.  
  206.         /* Update state variables here */
  207.  
  208.         if (In_string) {
  209.         switch(*pointer) {
  210.             case '\\':
  211.                 if (++cursor_pos > max_line_len) {
  212.                     cursor_pos -= 2;
  213.                     --pointer;
  214.                     goto overflow;
  215.                     }
  216.                 ++pointer;
  217.                 break;
  218.             case '"':
  219.                 In_string = 0;
  220.                 word_start = 0;
  221.             }
  222.         }
  223.         else switch (*pointer) {
  224.             case '"':
  225.             if (cursor_pos + 5 > max_line_len) {
  226.                 word_start = 0;
  227.                 --pointer;
  228.                 goto overflow;
  229.                 }
  230.             In_string = 1;
  231.             string_start = word_start = pointer;
  232.                 break;
  233.             case '\'':
  234.             if (pointer[1] == '\\')
  235.                 if ((ch = pointer[2]) >= '0' && ch <= '7')
  236.                     for(inc = 3; pointer[inc] != '\''
  237.                         && ++inc < 5;);
  238.                 else
  239.                     inc = 3;
  240.             else
  241.                 inc = 2;
  242.             /*debug*/ if (pointer[inc] != '\'')
  243.             /*debug*/  fatalstr("Bad character constant %.10s",
  244.                     pointer);
  245.             if ((cursor_pos += inc) > max_line_len) {
  246.                 cursor_pos -= inc;
  247.                 word_start = 0;
  248.                 --pointer;
  249.                 goto overflow;
  250.                 }
  251.             word_start = pointer;
  252.             pointer += inc;
  253.             break;
  254.         case '\t':
  255.             cursor_pos = 8 * ((cursor_pos + 8) / 8) - 1;
  256.             break;
  257.         default: {
  258.  
  259. /* HACK  Assumes that all characters in an atomic C token will be written
  260.    at the same time.  Must check for tokens first, since '-' is considered
  261.    part of an identifier; checking isident first would mean breaking up "->" */
  262.  
  263.             if (!word_start && isident(*(unsigned char *)pointer))
  264.             word_start = pointer;
  265.             else if (word_start && isntident(*(unsigned char *)pointer))
  266.             word_start = NULL;
  267.             break;
  268.         } /* default */
  269.         } /* switch */
  270.         cursor_pos++;
  271.     } /* for pointer = next_slot */
  272.  overflow:
  273.     if (*pointer == '\0') {
  274.  
  275. /* The output line is not complete, so break out and don't output
  276.    anything.  The current line fragment will be stored in the buffer */
  277.  
  278.         next_slot = pointer;
  279.         break;
  280.     } else {
  281.         char last_char;
  282.         int in_string0 = In_string;
  283.  
  284. /* If the line was too long, move   pointer   back to the character before
  285.    the current word.  This allows line breaking on word boundaries.  Make
  286.    sure that 80 character comment lines get broken up somehow.  We assume
  287.    that any non-string 80 character identifier must be in a comment.
  288. */
  289.  
  290.         if (word_start && *pointer != '\n' && word_start > output_buf)
  291.         if (In_string)
  292.             if (string_start && pointer - string_start < 5)
  293.                 pointer = string_start - 1;
  294.             else {
  295.                 pointer = adjust_pointer_in_string(pointer);
  296.                 string_start = 0;
  297.                 }
  298.         else if (word_start == string_start
  299.                 && pointer - string_start >= 5) {
  300.             pointer = adjust_pointer_in_string(next_slot);
  301.             In_string = 1;
  302.             string_start = 0;
  303.             }
  304.         else
  305.             pointer = word_start - 1;
  306.         else if (cursor_pos > max_line_len) {
  307.         extern char *strchr();
  308.         if (In_string) {
  309.             pointer = adjust_pointer_in_string(pointer);
  310.             if (string_start && pointer > string_start)
  311.                 string_start = 0;
  312.             }
  313.         else if (strchr("&*+-/<=>|", *pointer)
  314.             && strchr("!%&*+-/<=>^|", pointer[-1])) {
  315.             pointer -= 2;
  316.             if (strchr("<>", *pointer)) /* <<=, >>= */
  317.                 pointer--;
  318.             }
  319.         else
  320.             pointer--;
  321.         }
  322.         last_char = *pointer;
  323.         write_indent(fp, use_indent, extra_indent, output_buf, pointer);
  324.         next_slot = output_buf;
  325.         if (In_string && !string_start && Ansi == 1 && last_char != '\n')
  326.         *next_slot++ = '"';
  327.         fwd_strcpy(next_slot, pointer + 1);
  328.  
  329. /* insert a line break */
  330.  
  331.         if (last_char == '\n') {
  332.         if (In_string)
  333.             last_was_newline = 0;
  334.         else {
  335.             last_was_newline = 1;
  336.             extra_indent = 0;
  337.             }
  338.         }
  339.         else {
  340.         extra_indent = TOO_LONG_INDENT;
  341.         if (In_string && !string_start) {
  342.             if (Ansi == 1) {
  343.                 fprintf(fp, "\"\n");
  344.                 use_indent = 1;
  345.                 last_was_newline = 1;
  346.                 }
  347.             else {
  348.                 fprintf(fp, "\\\n");
  349.                 last_was_newline = 0;
  350.                 }
  351.             In_string = in_string0;
  352.             }
  353.         else {
  354.             putc ('\n', fp);
  355.             last_was_newline = 1;
  356.             }
  357.         } /* if *pointer != '\n' */
  358.  
  359.         if (In_string && Ansi != 1 && !string_start)
  360.         cursor_pos = 0;
  361.         else
  362.         set_cursor = 1;
  363.  
  364.         string_start = word_start = NULL;
  365.  
  366.     } /* else */
  367.  
  368.     } while (*next_slot);
  369.  
  370.     return 0;
  371. } /* ind_printf */
  372.